import { actNodeTypeEnum, signatureTypeEnum } from '@/utils/constant'
import { staticUrl } from '@/config'

export default {
  created() {
    console.log('this.signatures', this.signatures)
    if (!this.signatures.length) {
      return
    }
    this.operatorManagerAcceptConfessionsSignatures = this.signaturesHandler(
      signatureTypeEnum.OPERATION_MANAGER_ACCEPT_CONFESSIONS
    )
    this.operatorAcceptConfessionsSignatures = this.signaturesHandler(signatureTypeEnum.OPERATOR_ACCEPT_CONFESSIONS)
    this.guardianSignatures = this.signaturesHandler(signatureTypeEnum.GUARDIAN)
    this.unitOpinionSignatures = this.signaturesHandler(signatureTypeEnum.UNIT_OPINION)
    this.directorSignatures = this.signaturesHandler(signatureTypeEnum.DIRECTOR)
    this.riggerSignatures = this.signaturesHandler(signatureTypeEnum.RIGGER)
    this.operatorSignatures = this.signaturesHandler(signatureTypeEnum.OPERATOR)
    this.powerCustomerSignatures = this.signaturesHandler(signatureTypeEnum.POWER_CUSTOMER)
    this.operationManagerSignatures = this.signaturesHandler(signatureTypeEnum.OPERATION_MANAGER)
    this.projectManagerSignatures = this.signaturesHandler(signatureTypeEnum.PROJECT_MANAGER)
    this.maintenancerSignatures = this.signaturesHandler(signatureTypeEnum.MAINTENANCER)
    this.preelectricianSignatures = this.signaturesHandler(signatureTypeEnum.PREELECTRICIAN)
  },
  computed: {
    measuresSignature() {
      if (!this.inspect || !this.inspect[actNodeTypeEnum.CS]) {
        return []
      }
      const result = []
      this.inspect[actNodeTypeEnum.CS].forEach((item) => {
        item.signatures.forEach((item2) => {
          const signatureUrls = item2.signatureUrls.split(',')
          result.push(...signatureUrls)
        })
      })
      return result
    },
    // 措施数据
    measuresData() {
      if (!this.inspect || !this.inspect[actNodeTypeEnum.CS]) {
        return []
      }
      const result = []
      this.inspect[actNodeTypeEnum.CS].forEach((item) => {
        item.signatures.forEach((item2) => {
          item.signatureUrls = item2.signatureUrls.split(',')
        })
        result.push({
          ...item
        })
      })
      console.log('result', result)
      return result
    },
    // 交底签字数据
    disclosureSignatures() {
      if (!this.inspect || !this.inspect[actNodeTypeEnum.JD]) {
        return []
      }
      return this.signaturesFunc(this.inspect[actNodeTypeEnum.JD])
    },
    // 接受交底数据
    acceptDisclosureSignatures() {
      if (!this.inspect || !this.inspect[actNodeTypeEnum.JSJD]) {
        return []
      }
      const result = this.signaturesFunc(this.inspect[actNodeTypeEnum.JSJD])
      // 需要把执行时新增的作业人签名补充进来，bizSubId为空表示是新增的
      const data = this.signatures.filter((pet) => pet.signatureType === signatureTypeEnum.OPERATOR && !pet.bizSubId)
      data.forEach((item) => {
        const arr = item.signatureUrls?.split(',') ?? []
        result.push(...arr)
      })
      return result
    },
    // 审批
    approvalData() {
      const result = []
      if (this.inspect && this.inspect[actNodeTypeEnum.SP]) {
        // 监护人意见审批过滤掉
        result.push(
          ...this.groupBy(this.inspect[actNodeTypeEnum.SP].filter((pet) => pet.ticketNodeName.indexOf('监护人') === -1))
        )
      }
      if (this.inspect && this.inspect[actNodeTypeEnum.TJSP]) {
        result.push(...this.groupBy(this.inspect[actNodeTypeEnum.TJSP]))
      }
      return result
    },
    // 作业负责人数据
    operationChargeData() {
      const result = []
      if (this.inspect && this.inspect[actNodeTypeEnum.JSJD]) {
        result.push(...this.groupBy(this.inspect[actNodeTypeEnum.JSJD]))
      }
      return result
    },
    // 验票
    checkData() {
      if (!this.inspect || !this.inspect[actNodeTypeEnum.YP]) {
        return []
      }
      return this.groupBy(this.inspect[actNodeTypeEnum.YP])
    },
    // 开工
    operationData() {
      if (!this.inspect || !this.inspect[actNodeTypeEnum.KG]) {
        return []
      }
      return this.groupBy(this.inspect[actNodeTypeEnum.KG])
    },
    // 完工
    completeData() {
      if (!this.inspect || !this.inspect[actNodeTypeEnum.YS]) {
        return []
      }
      return this.groupBy(this.inspect[actNodeTypeEnum.YS])
    }
  },
  data() {
    return {
      staticUrl,
      unitOpinionSignatures: [], // 所在单位意见
      operatorAcceptConfessionsSignatures: [], // 作业人接受交底
      operatorManagerAcceptConfessionsSignatures: [], //作业负责人接受交底
      guardianSignatures: [], // 监护人
      riggerSignatures: [], // 司索人
      directorSignatures: [], // 指挥人
      operatorSignatures: [], // 作业人
      powerCustomerSignatures: [], // 用电人
      operationManagerSignatures: [], // 作业负责人
      projectManagerSignatures: [], //  项目负责人
      maintenancerSignatures: [], // 检修人
      preelectricianSignatures: [] // 专业电工
    }
  },
  methods: {
    handlePreviewImage(url, urls) {
      let current = 0
      if (urls) {
        urls = urls.map((item) => `${staticUrl}${item}`)
        current = urls.indexOf(url)
      }
      uni.previewImage({
        urls: urls ?? [url],
        current
      })
    },
    groupBy(data) {
      const result = []
      data.forEach((item) => {
        const node = result.find((pet) => pet.ticketNodeId === item.ticketNodeId)
        item.signatureUrls = []
        item.signatures.forEach((item2) => {
          const signatureUrls = item2.signatureUrls.split(',')
          item.signatureUrls.push(...signatureUrls)
        })
        item.uploadUrl = []
        item.content = ''
        item.acceptanceList?.forEach((item3, index) => {
          if (index === 0) {
            item.content += item3.type === '1' ? '完工验收：' : ' 未完工结束：'
          }
          const Urls = item3.pictUrl ? item3.pictUrl.split(',') : []
          item.uploadUrl.push(...Urls)
          item.content += item3.acceptInfo ?? '' + '；'
        })
        if (node) {
          node.data.push(item)
        } else {
          result.push({
            ticketNodeId: item.ticketNodeId,
            ticketNodeName: item.ticketNodeName,
            ticketNodeType: item.ticketNodeType,
            data: [{ ...item }]
          })
        }
      })
      return result
    },
    signaturesHandler(signatureType) {
      const data = this.signatures.filter((pet) => pet.signatureType === signatureType)
      const result = []
      data.forEach((item) => {
        const arr = item.signatureUrls?.split(',') ?? []
        result.push(...arr)
      })
      return result
    },
    signaturesFunc(data) {
      const result = []
      data.forEach((item) => {
        item.signatures.forEach((item2) => {
          const signatureUrls = item2.signatureUrls.split(',')
          result.push(...signatureUrls)
        })
      })
      return result
    }
  }
}
